package com.eshipenet.shipnet.equipmentdatahub

import scala.util.Try

sealed trait EquipmentAction {
  val id: String
}
case class EquipmentInsert(after: EquipmentWithModel) extends EquipmentAction {
  override val id: String = after.id
}

case class EquipmentUpdate(before: EquipmentWithModel, after: EquipmentWithModel) extends EquipmentAction {
  override val id: String = after.id
}
case class EquipmentDelete(before: EquipmentWithModel) extends EquipmentAction {
  override val id: String = before.id
}

object EquipmentAction {

  def apply(act: DebeziumParser.Action): Try[EquipmentAction] = act match {
    case DebeziumParser.Insert(after) =>
      EquipmentWithModel(after).map(EquipmentInsert)
    case DebeziumParser.Update(before, after) =>
      Try {
        (EquipmentWithModel(before).get, EquipmentWithModel(after).get)
      }.map(v => EquipmentUpdate(v._1, v._2))
    case DebeziumParser.Delete(before) =>
      EquipmentWithModel(before).map(EquipmentDelete)
  }

}
